﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

enum MoveSelect
{
    MOUSE, KEYBOARD
}

class DaySelectionScreen : GameScreen
{
    private Day maxDay;

    SpriteFont font;

    MoveSelect notmoving = MoveSelect.KEYBOARD;

    private Texture2D back, chara, day0, day1, day2, title, mouseselection, mousecursor;
    private Vector2 day0v = new Vector2(140, 330), day1v = new Vector2(440, 360), day2v = new Vector2(740, 340), charav = new Vector2(800, 60);
    private Vector2 mouse;

    string name;
    int charisma, intelligence, perception;

    int selected = 0;

    bool load = false;

    Color[] charaColor, day0Color, day1Color, day2Color, pixel;

    /*public DaySelectionScreen(Day day) : base()
    {
        this.maxDay = day;
        load = true;

        MouseState state = Mouse.GetState();
        mouse.X = state.X - 20;
        mouse.Y = state.Y;
    }*/

    public DaySelectionScreen(Day day, string name, int charisma, int intelligence, int perception) : base()
    {
        maxDay = day;
        this.name = name;
        this.charisma = charisma;
        this.intelligence = intelligence;
        this.perception = perception; 
        
        MouseState state = Mouse.GetState();
        mouse.X = state.X - 20;
        mouse.Y = state.Y;      
    }

    public override void Draw(GameTime gameTime)
    {
        base.Draw(gameTime);

        SpriteBatch spriteBatch = ScreenManager.SpriteBatch;

        spriteBatch.Begin();

        //spriteBatch.GraphicsDevice.Clear(Color.LightGray);

        spriteBatch.Draw(back, Vector2.Zero, Color.White);
        spriteBatch.Draw(title, new Vector2(270, 50), Color.White);

        /*if (selected == 0)
            spriteBatch.Draw(chara, charav, Color.Gold);
        else*/
        spriteBatch.Draw(chara, charav, Color.White);


        spriteBatch.DrawString(font, name, new Vector2(charav.X + 20, charav.Y + 50), Color.Black);
        spriteBatch.DrawString(font, "Carisma: " + charisma, new Vector2(charav.X + 40, charav.Y + 100), Color.Black);
        spriteBatch.DrawString(font, "Inteligência: " + intelligence, new Vector2(charav.X + 40, charav.Y + 130), Color.Black);
        spriteBatch.DrawString(font, "Percepção: " + perception, new Vector2(charav.X + 40, charav.Y + 160), Color.Black);


        spriteBatch.Draw(day0, day0v, Color.White);

        if ((int)maxDay >= (int)Day.ZERO)
            if (selected == 0)
                spriteBatch.Draw(day0, day0v, Color.Gold);
            else
                spriteBatch.Draw(day0, day0v, Color.White);

        if ((int)maxDay >= (int)Day.ONE)
            if (selected == 1)
                spriteBatch.Draw(day1, day1v, Color.Gold);
            else
                spriteBatch.Draw(day1, day1v, Color.White);

        if ((int)maxDay >= (int)Day.TWO)
            if (selected == 2)
                spriteBatch.Draw(day2, day2v, Color.Gold);
            else
                spriteBatch.Draw(day2, day2v, Color.White);

        if(selected == -1)
            spriteBatch.Draw(mousecursor, mouse, Color.White);
        else
            spriteBatch.Draw(mouseselection, mouse, Color.White);

        /*
        spriteBatch.DrawString(font, "Aperte I para ir para a Intro", new Vector2(200, 50), Color.Black);

        for (int i = 1; i <= 5; i++)
        {
            if((int)maxDay >= i)
                spriteBatch.DrawString(font, "Aperte " + i + " para ir para o dia " + i, new Vector2(200, 50 + i * 50), Color.Black);
            else
                spriteBatch.DrawString(font, "Aperte " + i + " para ir para o dia " + i, new Vector2(200, 50 + i * 50 ), Color.White);
        }*/

        spriteBatch.End();
    }

    public override void LoadContent()
    {
        base.LoadContent();

        font = ScreenManager.Game.Content.Load<SpriteFont>("SpriteFont1");

        back = ScreenManager.Game.Content.Load<Texture2D>("Textures/Screen/notice board");
        title = ScreenManager.Game.Content.Load<Texture2D>("Textures/Screen/title");
        chara = ScreenManager.Game.Content.Load<Texture2D>("Textures/Screen/char");
        day0 = ScreenManager.Game.Content.Load<Texture2D>("Textures/Screen/dia0");
        day1 = ScreenManager.Game.Content.Load<Texture2D>("Textures/Screen/dia1");
        day2 = ScreenManager.Game.Content.Load<Texture2D>("Textures/Screen/dia2");
        mouseselection = ScreenManager.Game.Content.Load<Texture2D>("Textures/Screen/mouse");
        mousecursor = ScreenManager.Game.Content.Load<Texture2D>("Textures/Screen/mousecursor");


        charaColor = new Color[chara.Width * chara.Height];
        chara.GetData(0, new Rectangle(0, 0, chara.Width, chara.Height), charaColor, 0, chara.Width * chara.Height);

        day0Color = new Color[day0.Width * day0.Height];
        day0.GetData(0, new Rectangle(0, 0, day0.Width, day0.Height), day0Color, 0, day0.Width * day0.Height);

        day1Color = new Color[day1.Width * day1.Height];
        day1.GetData(0, new Rectangle(0, 0, day1.Width, day1.Height), day1Color, 0, day1.Width * day1.Height);

        day2Color = new Color[day2.Width * day2.Height];
        day2.GetData(0, new Rectangle(0, 0, day2.Width, day2.Height), day2Color, 0, day2.Width * day2.Height);

    }

    public override void HandleInput()
    {
        base.HandleInput();

        if (InputManager.IsActionTriggered(InputManager.Action.CursorDown) 
            || InputManager.IsActionTriggered(InputManager.Action.CursorLeft))
        {
            selected++;
            if (selected > (int)maxDay)
                selected = 0;
            notmoving = MoveSelect.KEYBOARD;
        }
        if (InputManager.IsActionTriggered(InputManager.Action.CursorUp) 
            || InputManager.IsActionTriggered(InputManager.Action.CursorRight))
        {
            selected--;
            if (selected < 0)
                selected = (int) maxDay;
            notmoving = MoveSelect.KEYBOARD;
        }
        if (InputManager.IsActionTriggered(InputManager.Action.Ok))
        {
            if (selected < 0)
                return;
            /*if (selected == 0)
            {
                CharacterCreationScreen characreation = new CharacterCreationScreen();

                characreation.Name = name;
                characreation.Charisma = charisma;
                characreation.Perception = perception;
                characreation.Intelligence = intelligence;
                characreation.Day = maxDay;
                characreation.Points = 0;
                characreation.Edit = true;

                ScreenManager.AddScreen(characreation);

                Exit();
                return;
            }*/
            if (selected == 0)
            {
                if (!load) ScreenManager.AddScreen(new GameplayScreen(Day.ZERO, maxDay, name, charisma, intelligence, perception));
                else GameSession.LoadDay(Day.ZERO);

                Exit();
                return;
            }
            if (selected == 1)
            {
                if (!load) ScreenManager.AddScreen(new GameplayScreen(Day.ONE, maxDay, name, charisma, intelligence, perception));
                else GameSession.LoadDay(Day.ONE);

                Exit();
                return;
            }
            if (selected == 2)
            {
                if (!load) ScreenManager.AddScreen(new GameplayScreen(Day.TWO, maxDay, name, charisma, intelligence, perception));
                else GameSession.LoadDay(Day.TWO);

                Exit();
                return;
            }
        }
        if (InputManager.IsActionTriggered(InputManager.Action.Back) && GameSession.Player == null)
        {
            Exit();
        }
    }

    private void Exit()
    {
        ExitScreen();
    }

    public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
    {
        MouseState state = Mouse.GetState();

        if (!(mouse.X + 5 > state.X - 20 && mouse.X - 5 < state.X - 20) || !(mouse.Y + 5 > state.Y && mouse.Y - 5 < state.Y))
        {
            mouse.X = state.X - 20;
            mouse.Y = state.Y;
            notmoving = MoveSelect.MOUSE;
        }

        base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);

        if (pixel == null)
        {
            pixel = new Color[5 * 5];
            for (int i = 0; i < pixel.Length; i++)
                pixel[i] = Color.Black;
        }

        if(notmoving == MoveSelect.MOUSE)
        /*if (IntersectPixels(new Rectangle((int)charav.X, (int)charav.Y, chara.Width, chara.Height), charaColor,
            new Rectangle((int)mouse.X + 20, (int)mouse.Y, 5, 5), pixel))
            selected = 0;
        else*/
        if (IntersectPixels(new Rectangle((int)day0v.X, (int)day0v.Y, day0.Width, day0.Height), day0Color,
            new Rectangle((int)mouse.X + 20, (int)mouse.Y, 5, 5), pixel))
            selected = 0;
        else
        if (IntersectPixels(new Rectangle((int)day1v.X, (int)day1v.Y, day1.Width, day1.Height), day1Color,
            new Rectangle((int)mouse.X + 20, (int)mouse.Y, 5, 5), pixel))
            selected = 1;
        else
        if (IntersectPixels(new Rectangle((int)day2v.X, (int)day2v.Y, day2.Width, day2.Height), day2Color,
            new Rectangle((int)mouse.X + 20, (int)mouse.Y, 5, 5), pixel))
            selected = 2;
        else
            selected = -1;

        if (state.LeftButton == ButtonState.Pressed)
        {
            if (selected < 0)
                return;
            /*if (selected == 0)
            {
                CharacterCreationScreen characreation = new CharacterCreationScreen();

                characreation.Name = name;
                characreation.Charisma = charisma;
                characreation.Perception = perception;
                characreation.Intelligence = intelligence;
                characreation.Day = maxDay;
                characreation.Points = 0;
                characreation.Edit = true;

                ScreenManager.AddScreen(characreation);

                Exit();
                return;
            }*/
            if (selected == 0)
            {
                if (!load) ScreenManager.AddScreen(new GameplayScreen(Day.ZERO, maxDay, name, charisma, intelligence, perception));
                else GameSession.LoadDay(Day.ZERO);

                Exit();
                return;
            }
            if (selected == 1)
            {
                if (!load) ScreenManager.AddScreen(new GameplayScreen(Day.ONE, maxDay, name, charisma, intelligence, perception));
                else GameSession.LoadDay(Day.ONE);

                Exit();
                return;
            }
            if (selected == 2)
            {
                if (!load) ScreenManager.AddScreen(new GameplayScreen(Day.TWO, maxDay, name, charisma, intelligence, perception));
                else GameSession.LoadDay(Day.TWO);

                Exit();
                return;
            }
        }

    }


    public bool IntersectPixels(Rectangle rectangleA, Color[] dataA, Rectangle rectangleB, Color[] dataB)
    {
        int top = Math.Max(rectangleA.Top, rectangleB.Top);
        int bottom = Math.Min(rectangleA.Bottom, rectangleB.Bottom);
        int left = Math.Max(rectangleA.Left, rectangleB.Left);
        int right = Math.Min(rectangleA.Right, rectangleB.Right);

        for (int y = top; y < bottom; y++)
        {
            for (int x = left; x < right; x++)
            {
                Color colorA = dataA[(x - rectangleA.Left) +
                                     (y - rectangleA.Top) * rectangleA.Width];
                Color colorB = dataB[(x - rectangleB.Left) +
                                     (y - rectangleB.Top) * rectangleB.Width];

                if (colorA.A != 0 && colorB.A != 0)
                {
                    return true;
                }
            }
        }
        return false;
    }
}
